import {login, getUserInfo, listUser, removeUser, updateRemark, updateUser, addUser} from '@/api/system/user'
import {setToken, getToken, removeToken, _Promise} from '@/libs/util'
import role from './user/role'
import Cookies from 'js-cookie'
import Vue from 'vue'

export default {
  namespaced: true,
  state: {
    userName: '',
    userId: '',
    avatorImgPath: '',
    token: getToken(),
    // 用户集合
    userList: []
  },
  mutations: {
    setAvator(state, avatorPath) {
      state.avatorImgPath = avatorPath
    },
    setUserId(state, id) {
      state.userId = id
    },
    setUserName(state, name) {
      state.userName = name
    },
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    setUserList(state, userList) {
      state.userList = userList
    },
    setAccess(state, {modules, functions}) {
      Cookies.set('modules', modules)
      Cookies.set('functions', functions)
    },
    removeAccess() {
      Cookies.remove('modules')
      Cookies.remove('functions')
    },
    removeUser(state, userId) {
      state.userList.forEach((user, index) => {
        if (user.id === userId) {
          state.userList.splice(index, 1)
        }
      })
    },
    updateRemark(state, user) {
      state.userList.forEach(existsUser => {
        if (existsUser.id === user.id) {
          existsUser.remark = user.remark
        }
      })
    },
    updateUser(state, user) {
      for (let i = 0; i < state.userList.length; i++) {
        let existsUser = state.userList[i];
        if (existsUser.id === user.id) {
          let keys = Object.keys(existsUser);
          keys.forEach(key => {
            Vue.set(existsUser, key, user[key]);
          })
          break
        }
      }
    },
    addUser(state, user) {
      state.userList.unshift(user)
    }
  },
  actions: {
    // 登录
    handleLogin({commit}, {username, password}) {
      username = username.trim()
      return _Promise(login({username, password}), res => {
        let data = res.data;
        commit('setToken', data.token)
        commit('setAccess', {
          modules: data.modules,
          functions: data.functions
        })
        Cookies.set('username', username)
      })

    },
    // 退出登录
    handleLogout({commit}) {
      commit('removeToken')
      commit('removeAccess')
      Cookies.remove('username')
      localStorage.removeItem("tagNaveList")
    },
    // 获取用户相关信息
    getUserInfo({state, commit}) {
      return _Promise(getUserInfo(state.token), res => {
        const data = res.data
        commit('setAvator', data.avator)
        commit('setUserName', data.user_name)
        commit('setUserId', data.user_id)
      })
    }
    ,

    // 获取所有用户信息
    listUser({state, commit}) {
      return _Promise(listUser(), res => {
        commit('setUserList', res.data)
      })
    }
    ,

    // 更新备注
    updateRemark({state, commit}, user) {
      return _Promise(updateRemark(user), res => {
        commit('updateRemark', res.data)
      })
    }
    ,

    // 更新用户信息
    updateUser({state, commit, dispatch}, user) {
      return _Promise(updateUser(user), res => {
        commit('updateUser', res.data)
      })
    }
    ,

    // 删除用户
    removeUser({state, commit}, userId) {
      return _Promise(removeUser(userId), () => {
        commit('removeUser', userId)
      })
    }
    ,

    // 添加用户
    addUser({state, commit}, user) {
      return _Promise(addUser(user), res => {
        commit('addUser', res.data)
      })
    }
  }
  ,
  modules: {
    role
  }
}
